/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.gui.taxonomytree.rendering;

import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;

/**
 *
 * @author Kenneth
 */
public class DefaultTaxonomyTreeModel extends DefaultTreeModel {

    public DefaultTaxonomyTreeModel() {
        this(null);
    }

    public DefaultTaxonomyTreeModel(TreeNode root) {
        super(root);
    }

    @Override
    public void insertNodeInto(MutableTreeNode child, MutableTreeNode parent, int amountChildren) {
        //set the icon of the fresh node
        //for now : get the first letter of the superkingdom (the 3rd node is always there...)
        if (child instanceof DefaultTaxonomyTreeNode) {
            if (!child.toString().toLowerCase().contains("show organism")) {
                DefaultTaxonomyTreeNode temp = (DefaultTaxonomyTreeNode) child;
                TreeNode[] path = ((DefaultMutableTreeNode) parent).getPath();
                String iconName = "*";
                if (path.length >= 3) {
                    iconName = path[2].toString().substring(0, 1);
                }
                Icon icon = (Icon) TaxonomyTreeIconFactory.getInstance().getIcon(iconName);
                temp.setIcon(icon);
                super.insertNodeInto(child, parent, amountChildren);
            }
        }
    }

}
